﻿using Base.ClientUI;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading;

namespace GK.WCS.Client.Station
{
    public class HttpCraneUtil
    {
        public static String baseUrl;
        static HttpCraneUtil()
        {
            baseUrl = ConfigurationManager.AppSettings["wcsServer"];
        }
        public static CraneStatusGk loadData(int CraneId)
        {
            JObject data = WCSHttpUtil.getObject(baseUrl + "Crane/craneStates/" + CraneId);

            if (data == null)
            {
                return null;
            }
            CraneStatusGk GkDYGCraneStatus = JsonConvert.DeserializeObject<CraneStatusGk>(data.ToString());
            return GkDYGCraneStatus;
        }

       
        ///Crane/ReConnectRobot/craneId/mode
        static public bool EmergencyStop(int CraneId)
        {
            JValue data = WCSHttpUtil.getJValue(baseUrl + "Crane/EmergencyStop/" + CraneId);
            if (data == null)
            {
                return false;
            }
            return bool.Parse(data.ToString());

        }

        static public bool Reset(int CraneId)
        {
            JValue data = WCSHttpUtil.getJValue(baseUrl + "Crane/Reset/" + CraneId);
            if (data == null)
            {
                return false;
            }
            return bool.Parse(data.ToString());

        }
        ///Crane/sendTask/craneId/forkNo/src/des/actionid
        static public bool sendTask(String CraneId, String forkNo, String taskType, String fromPlcShelf, String fromPlcRow, String fromPlcCol,string toPlcShelf,string toPlcRow,string toPlcCol,int src,int des)
        {

            JValue data = WCSHttpUtil.getJValue(baseUrl + "Crane/sendTask/" + CraneId + "/" + forkNo + "/" + taskType + "/" + fromPlcShelf + "/" + fromPlcRow + "/" + fromPlcCol + "/" + toPlcShelf + "/" + toPlcRow + "/" + toPlcCol +
                "/" + src + "/" + des);

            if (data == null)
            {
                return false;
            }
            try
            {
               return bool.Parse(data.ToString());
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        static public String sendTaskLoc( String src, String des) {

            JValue data = WCSHttpUtil.getJValue(baseUrl + "Crane/sendTaskLoc/" +   src + "/" + des);

            if (data == null) {
                return "异常访问";
            }
       
           return  data.ToString();
            
        }



        static public bool ClearCraneTask(int CraneId)
        {
            JValue data = WCSHttpUtil.getJValue(baseUrl + "Crane/ClearCraneTask/" + CraneId);
            if (data == null)
            {
                return false;
            }
            return bool.Parse(data.ToString());

        }

        static public bool ClearCraneFault(int CraneId)
        {
            JValue data = null;
            for (int i = 0; i < 3; i++) {
                data = WCSHttpUtil.getJValue(baseUrl + "Crane/clearLed");
                if (data == null)
                {
                    return false;
                }
                Thread.Sleep(100);
            }
           
            return bool.Parse(data.ToString());
        }

        
    }
}
